package com.boot.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.boot.controller.myhandler.HandlerException;
import com.entities.CommonResult;
import com.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description:
 *
 * @Autor: Rolin
 * datetime: 05-07-20:46
 */
@RestController
public class SentinelController {

    @GetMapping("/testA")
    public String testA() {
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "------testB";
    }

    @GetMapping("/testD")
    public String testD() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "------testD";
    }

//    服务预热测试

    @GetMapping("/hotkey")
    @SentinelResource(value = "p1",blockHandler = "deal_block")
    public String test_key(@RequestParam(value = "p1",required = false)String p1,
                           @RequestParam(value = "p2",required = false)String p2){

        return "key_test，p1="+p1+"\tp2="+p2;
    }
//  必须要传入 BlockException exception 型参
    public String deal_block(String p1, String p2, BlockException exception){
        return "服务阻塞，请稍后再试！";
    }

//    解耦 controller类
    @GetMapping("/diyBlockException")
    @SentinelResource(value = "diy",blockHandlerClass = HandlerException.class,blockHandler = "test1")
    public CommonResult testForDiy(){
        return new CommonResult("自定义sentinel异常处理",200,new Payment(200L,"吃馒头"));
    }


}
